package com.xiezhr.pbideaapitestdemo.service.impl;

import com.xiezhr.pbideaapitestdemo.entity.ApiResponse;
import com.xiezhr.pbideaapitestdemo.entity.Student;
import com.xiezhr.pbideaapitestdemo.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: pbideaApiTestDemo
 * @description:
 * @author: xie.zhr
 * @create: 2024-12-07 11:26
 **/
@Service
public class StudentServiceImpl implements StudentService {

    private List<Student> students;


    public  StudentServiceImpl() {
        students = new ArrayList<>();
        // 添加一些示例数据
        students.add(new Student("S001", "晓凡", 18, "男", "计算机科学", "xiezhr@qq.com", "13800000001"));
        students.add(new Student("S002", "李四", 20, "女", "电子工程", "lisi@example.com", "13800000002"));
        students.add(new Student("S003", "王五", 19, "男", "机械工程", "wangwu@example.com", "13800000003"));
        students.add(new Student("S004", "张三", 19, "男", "机械工程", "zhangsan@example.com", "13800000003"));
    }

    /**
     * 获取所有学生信息
     * @return
     */
    @Override
    public List<Student> getAllStudents() {
        return students;
    }

    /**
     * 根据id获取学生信息
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(String id) {
        return students.stream()
                .filter(student -> student.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    /**
     * 添加学生信息
     * @param student
     * @return
     */
    @Override
    public ApiResponse<Student> addStudent(Student student) {
        students.add(student);
        return new ApiResponse<>(201, "学生添加成功", student);
    }
}
